const mongoose = require("mongoose");
const Ques = require("../models/QuesModel");

exports.addQues = async (req, res) => {
  try {
    const newQues = await Ques.create(req.body);
    res.status(201).json({
      status: "success",
      data: newQues,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.getQues = async (req, res) => {
  try {
    let time2 = new Date();
    // console.log("时间2---", time2.getMinutes() + ":" + time2.getSeconds());
    const QuesData = await Ques.find();
    let time = new Date();
    // console.log("时间3---", time.getMinutes() + ":" + time.getSeconds());
    // console.log("QuesData[0] ", QuesData[0]);
    if (typeof QuesData[0] == "undefined") {
      res_data = {
        target: -1,
        title: "",
        body: [],
      };
    } else {
      res_data = QuesData[0];
    }
    res.status(201).json({
      status: "success",
      data: res_data,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};

exports.updateQues = async (req, res) => {
  try {
    const QuesData = await Ques.updateOne({ _id: req.body._id }, req.body);
    res.status(201).json({
      status: "success",
      data: QuesData,
    });
  } catch (err) {
    console.log(err);
    res.status(404).json({
      status: true,
      err,
    });
  }
};
